package com.sunday.aomicaichang.ui.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.http.ApiClient;
import com.sunday.member.entity.Village;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/8.
 */

public class PopAreaWindow extends PopupWindow {
    private View conentView;
    LinearLayout lyArea;
    private OnOrderCreenPopConfirmListener popConfirmListener;
    public PopAreaWindow(final Activity context, final Context mContext, final OnOrderCreenPopConfirmListener popConfirmListener){
        this.popConfirmListener = popConfirmListener;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.pop_area, null);
        lyArea= (LinearLayout) conentView.findViewById(R.id.ly_area);
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(w / 2 + 40);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);

        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimationPreview);
        Call<ResultDO<List<Village>>> call=  ApiClient.getApiAdapter().getVillage();
        call.enqueue(new Callback<ResultDO<List<Village>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Village>>> call, Response<ResultDO<List<Village>>> response) {
                if (response.body()==null){return;}
                if (response.body().getCode()==0){
                    if (response.body().getResult()==null){return;}
                    final List<Village> villages = response.body().getResult();
                    lyArea.removeAllViews();
                    if (villages.size() != 0) {
                        for (final Village v2 : villages) {
                            View v1 = LayoutInflater.from(mContext).inflate(R.layout.item_area, null);
                            TextView area = (TextView) v1.findViewById(R.id.tv_area);
                            if (!TextUtils.isEmpty(v2.getName()))
                                area.setText(v2.getName());
                            area.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (popConfirmListener != null) {
                                    popConfirmListener.confirmClick(v2.getId());
                                }
                                    PopAreaWindow.this.dismiss();
                                }
                            });
                            lyArea.addView(v1);
                        }
                    }

                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<Village>>> call, Throwable t) {
            }
        });
//        conentView.findViewById(R.id.about).setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                //do something you need here
//                PopWindow.this.dismiss();
//            }
//        });
    }


    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
//            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 5);
            this.showAtLocation(parent, Gravity.CENTER,0,0);
        } else {
            this.dismiss();
        }
    }
    public interface OnOrderCreenPopConfirmListener {
        void confirmClick(int result);
    }
}
